package com.electronic.exception;


import com.alibaba.fastjson.JSON;
import com.electronic.api.R;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: IFCA
 * @Description: 自定义业务异常
 */
@Slf4j
public class ServiceException extends RuntimeException {

    private ResultCode errorCode;

    public ServiceException(String message) {
        super(message);
        this.errorCode = ResultCode.FAIL;
    }

    public ServiceException(ResultCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }

    public ServiceException(ResultCode errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
        log.error(JSON.toJSONString(R.fail(HttpServletResponse.SC_BAD_REQUEST, message,errorCode.getErrorCode())));
    }

    public ResultCode getErrorCode() {
        return errorCode;
    }

    public void setCode(ResultCode errorCode) {
        this.errorCode = errorCode;
    }

}
